import NGameMessage from "../../util/NGameMessage";
import JNetParam from "../entity/JNetParam";
import ToUtil from "./ToUtil";

//网络数据工具类
export default class NDataUtil{

    //解析
    static Parse(data):JNetParam{
        data = Array.from(new Uint8Array(data)) as number[];
        if(data.length < 8) return;
        return JNetParam.Build(ToUtil.Byte4ToInt([...data.slice(0,4)]),ToUtil.Byte4ToInt([...data.slice(4,8)])).setByte(data.slice(8,data.length))
    }

    //加密 [请求Id*4,处理Id*4,...参数数据*N]
    static Encrypt(data:JNetParam,type?:string):ArrayBuffer{
        let datas = Array.from(NGameMessage.getIns().DataToByte4(type,data.data));
        let byte = [...ToUtil.IntToByte4(data.id),...ToUtil.IntToByte4(data.hId),...datas];
        let array = new ArrayBuffer(byte.length);
        let datao = new DataView(array);
        for (let index = 0; index < byte.length; index++) {
            datao.setUint8(index,byte[index]);
        }
        return array;
    }

}

